package cn.edu.zjicm.wordsnet_d.data.test;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.RelationShip;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;

public class Question {

	private QuestionMode questionMode;
	private Context mContext;

	OneWord word = null;
	RelationShip relationShip = null;

	Question(OneWord word, Context mContext) {
		this.word = word;
		resetQuestionMode();
		this.mContext = mContext;
	}

	// 单词测试后选择搭配进行测试
	RelationShip getRelationship(OneWord word) {
		// 每次取不同的搭配，如果到最后，再从头开始取
		WordFactory wf = WordFactory.getInstance(mContext);
		int numOfColloc = wf.getNumOfRelationshipOfWord(word.getIndex_word_id());
		if (numOfColloc == 0)
			return null;
		int nodeIndex;
		if (word.getSense_degree_fm() == 0)
			nodeIndex = 0;
		else
			nodeIndex = (word.getSense_degree_fm() / 10 - 1) % numOfColloc;
		return wf.getRelationshipOfWordForTest(word.getIndex_word_id(),	nodeIndex);
	}

	public QuestionMode getQuestionMode() {
		return questionMode;
	}

	public void resetQuestionMode() {
		this.relationShip = getRelationship(word);
		if (this.relationShip != null){
			this.questionMode = QuestionMode.getQuestionMode(word.getSense_degree_fm(), word.getLemma_type());
//			LogUtil.d("Tag", word.getLemma()+",有搭配,sense_degree="+word.getSense_degree_fm()+",questionMode="+this.questionMode.oldDegree);
		}
			
		else{
			this.questionMode = QuestionMode.getQuestionModeWithoutRelationship(word.getSense_degree_fm(), word.getLemma_type());
//			LogUtil.d("Tag", word.getLemma()+",无搭配,sense_degree="+word.getSense_degree_fm()+",questionMode="+this.questionMode.oldDegree);
		}
	}

	public void setDegree_fmWhenRightAnswer() {
		int oldSenseDegree = word.getSense_degree_fm();
		int oldDegree = DataManager.getWordDegreeType(word.getSense_degree_fm());

		word.setSense_degree_fm(questionMode.getNextDegreeAfterRightAnswer());

		if (word.getSense_degree_fm() == 10) { // 将未学单词设为生词
			DataManager.getInstance().changeUnLearnToUnFamiliar(word, mContext,	false);
//			LogUtil.e("加为生词", word.getLemma());
		}

		if (word.getSense_degree_fm() < oldSenseDegree && word.getSense_degree_fm() % 10 == 0) { // 降级单词
			WordFactory.getInstance(mContext).markCurrentForgetWord(word);
		}

		WordFactory.getInstance(mContext).updateCurrentWordDegree(word);

		int newDegree = DataManager.getWordDegreeType(word.getSense_degree_fm());
//		Log.e("WordSenseType:" + word.getLemma(), oldDegree + "........" + newDegree);
		Log.e("WordSenseDegree:" + word.getLemma(), oldSenseDegree + "........"	+ word.getSense_degree_fm());
		// 如果单词熟悉度等级变为5，这里不需要修改统计，因为
		// DataManager.getInstance().changeUnFamiliarToFamiliar(word, mContext);会做
		if (oldDegree != newDegree && newDegree != OneWord.SENSE_DEGREE_FAMILIAR_TYPE) {
			AppPreference.setNewWordsFmDegree(mContext,
					AppPreference.getNewWordsFmDegree(mContext, oldDegree) - 1,
					oldDegree);
			AppPreference.setNewWordsFmDegree(mContext,
					AppPreference.getNewWordsFmDegree(mContext, newDegree) + 1,
					newDegree);
		}

		// 如果单词熟悉度等级变为5
		if (newDegree == OneWord.SENSE_DEGREE_FAMILIAR_TYPE) {
			OneWord w = DataManager.getInstance().findWord(word, mContext);
			if (w != null) {
				w.setSense_degree_fm(oldSenseDegree);
				DataManager.getInstance().changeUnFamiliarToFamiliar(w,
						mContext);
				Toast.makeText(mContext, w.getLemma() + "已升级为熟词",
						Toast.LENGTH_SHORT).show();
			}
		}		
	}

	void setDegree_fmWhenWrongAnswer() {
		int oldSenseDegree = word.getSense_degree_fm();
		int oldDegree = DataManager.getWordDegreeType(word.getSense_degree_fm());

		word.setSense_degree_fm(questionMode.getNextDegreeAfterWrongAnswer());

		if (word.getSense_degree_fm() == OneWord.SENSE_DEGREE_TOO_EASY) { // 将未学单词设为太简单
			DataManager.getInstance().changeUnLearnToTooEasy(word, mContext);
			Toast.makeText(mContext, word.getLemma() + "已升级为太简单", Toast.LENGTH_SHORT).show();
			Log.e("加为太简单", word.getLemma());
		}

		WordFactory.getInstance(mContext).updateCurrentWordDegree(word);
		
		if(questionMode.getTestMode() == 2) {
			WordFactory.getInstance(mContext).markCurrentForgetWord(word);
			Log.e("忘了", word.getLemma());
		}

		int newDegree = DataManager.getWordDegreeType(word.getSense_degree_fm());
//		Log.e("WordSenseType:" + word.getLemma(), oldDegree + "........" + newDegree);
		Log.e("WordSenseDegree:" + word.getLemma(), oldSenseDegree + "........"	+ word.getSense_degree_fm());
		// 如果单词熟悉度等级变为6（太简单），这里不需要修改统计，因为
		// DataManager.getInstance().changeUnLearnToTooEasy(word, mContext);会做
		if (oldDegree != newDegree && newDegree != OneWord.SENSE_DEGREE_TOO_EASY_TYPE) {
			AppPreference.setNewWordsFmDegree(mContext,
					AppPreference.getNewWordsFmDegree(mContext, oldDegree) - 1,
					oldDegree);
			AppPreference.setNewWordsFmDegree(mContext,
					AppPreference.getNewWordsFmDegree(mContext, newDegree) + 1,
					newDegree);
		}
	}

	public OneWord getWord() {
		return word;
	}

	public RelationShip getRelationShip() {
		return relationShip;
	}

	public String getPhonetic() {
		return word.getPhonetic();
	}

	public String getLemma() {
		return word.getLemma();
	}

	public String getCn() {
		return word.getSensesSenior();
	}

	public int getOldDegree() {
		return questionMode.getOldDegree();
	}

	public int getFamiliarDegree() {
		return word.getSense_degree_fm();
	}

	public int getID() {
		return word.getIndex_word_id();
	}

}

// int testType; //0 单词测试 1搭配测试
// int questionType; //0 英译中 1 中译英
// int round = 0;
// boolean everForget;
// int degreeChange = 0; //-1,0,1

// public int getDegreeChange() {
// return degreeChange;
// }
//
// public void setDegreeChange(int degreeChange) {
// this.degreeChange = degreeChange;
// }
//
// public boolean isEverForget() {
// return everForget;
// }
//
// public void setEverForget(boolean everForget) {
// this.everForget = everForget;
// }

// public int getTestType() {
// return testType;
// }
//
// public void setTestType(int testType) {
// this.testType = testType;
// }

// public int getRound() {
// return round;
// }
//
// public void setRound(int round) {
// this.round = round;
// }

// public int getQuestionType(){
// return questionType;
// }
//
// public void setQuestionType(int questionType){
// this.questionType = questionType;
// }

